/**
 * Created by admin on 2016-12-03.
 */
// 引入 gulp及组件
var gulp = require('gulp'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),		//压缩css
    uglify = require('gulp-uglify'),			//压缩JS
    imagemin = require('gulp-imagemin'),		//压缩图片
    rename = require('gulp-rename'),			//合并js文件
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    del = require('del');

gulp.task('uglify', function() {
    return gulp.src('src/main/webapp/web/WEB-INF/static/js/**.js')
        .pipe(concat('main.js'))    //合并所有js到main.js
        .pipe(gulp.dest('dist/scripts'))    //输出main.js到文件夹
        .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
        .pipe(uglify())    //压缩
        .pipe(gulp.dest('dist/scripts'));  //输出
});
// Styles
gulp.task('styles', function() {
   /* return gulp.src('src/styles/!*.css')
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss())
        .pipe(gulp.dest('dist/styles'))
        .pipe(notify({ message: 'Styles task complete' }));*/
    gulp.src('src/main/webapp/web/WEB-INF/static/css/**.css')    //- 需要处理的css文件，放到一个字符串数组里
        .pipe(concat('main.css'))                            //- 合并后的文件名
        .pipe(gulp.dest('dist/css'))    //输出main.css到文件夹
        .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
        .pipe(minifycss())    //压缩
        .pipe(gulp.dest('dist/css'));                              //- 将 rev-manifest.json 保存到 rev 目录内
});
// Scripts
gulp.task('scripts', function() {
    return gulp.src('src/main/webapp/web/WEB-INF/static/js/**.js')
        .pipe(concat('main.js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/scripts'))
        .pipe(notify({ message: 'Scripts task complete' }));
});
// Images
gulp.task('images', function() {
    return gulp.src('src/main/webapp/web/WEB-INF/static/images/**')
        .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
        .pipe(gulp.dest('dist/images'))
        .pipe(notify({ message: 'Images task complete' }));
});

// Clean  任务执行前，先清除之前生成的文件
gulp.task('clean', function(cb) {
    del(['dist/assets/css', 'dist/assets/js', 'dist/assets/img'], cb)
});

// Default task  设置默认任务
gulp.task('default', ['clean'], function() {
    gulp.start('styles', 'scripts', 'images');
});

// Watch
gulp.task('watch', function() {
    // Watch .scss files
    gulp.watch('src/main/webapp/web/*', ['styles']);
    // Watch .js files
    gulp.watch('src/main/webapp/web/*', ['scripts']);
    // Watch image files
    gulp.watch('src/main/webapp/web/*', ['images']);
    // Create LiveReload server
    livereload.listen();
    // Watch any files in dist/, reload on change
    gulp.watch(['dist/**']).on('change', livereload.changed);

});